package com.muqi.app.qlearn.student.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.iflytek.thridparty.bo;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.modles.Body;
import com.muqi.app.qlearn.student.BaseStudentActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.student.R.layout;
import com.muqi.app.user.db.ChildBean;
import com.muqi.app.user.widget.DashboardView;
import com.muqi.app.user.widget.DashboardView.StripeMode;
import com.muqi.app.user.widget.HighlightCR;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

public class HealthActivity extends BaseStudentActivity {

	private DashboardView mDHeight;
	private DashboardView mDWeight;

	private ImageView mIvWave;

	private TextView mTvH;
	private TextView mTvHBig;
	private TextView mTvHAdd;

	private TextView mTvW;
	private TextView mTvWBig;
	private TextView mTvWAdd;

	private Animation animation;
	private int[] mColors = new int[] { 0xFF4090CE, 0xFF17ACED };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_health);
		initView();
		loadData();
	}

	private void initView() {
		mDHeight = (DashboardView) findViewById(R.id.dashboard_height);
		mDWeight = (DashboardView) findViewById(R.id.dashboard_weight);
		mDHeight.setStripeMode(StripeMode.NORMAL);
		// mDHeight.setmColor(mColors);
		List<HighlightCR> highlight3 = new ArrayList<HighlightCR>();
		highlight3.add(new HighlightCR(150, 30, Color.parseColor("#242578")));
		highlight3.add(new HighlightCR(180, 40, Color.parseColor("#4090CE")));
		highlight3.add(new HighlightCR(220, 100, Color.parseColor("#17ACED")));
		highlight3.add(new HighlightCR(320, 40, Color.parseColor("#4090CE")));
		highlight3.add(new HighlightCR(0, 30, Color.parseColor("#242578")));
		mDHeight.setStripeHighlightColorAndRange(highlight3);
		mDWeight.setStripeMode(StripeMode.INNER);

		mTvH = (TextView) findViewById(R.id.health_tv_height);
		mTvHBig = (TextView) findViewById(R.id.health_tv_big);
		mTvHAdd = (TextView) findViewById(R.id.health_tv_grow);

		mTvW = (TextView) findViewById(R.id.health_tv_weight);
		mTvWBig = (TextView) findViewById(R.id.health_tv_big_weight);
		mTvWAdd = (TextView) findViewById(R.id.health_tv_add);

		mIvWave = (ImageView) findViewById(R.id.iv_wave);
		AnimationDrawable anim = (AnimationDrawable) mIvWave.getBackground();
		anim.start();

	}

	@Override
	protected void onChoiceChildResult(ChildBean myChild) {
		// TODO Auto-generated method stub

	}

	public void onBack(View view) {
		finish();
	}

	/**
	 * 加载身高和体重
	 */
	private void loadData() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("token", mSpUtil.getToken());
			map.put("user_id", mSpUtil.getUserId());
			String requestApi = ParamsUtils.buildParams(
					NetWorkApi.GET_BODY_API, map);
			MyAsyncHttp.get(this, requestApi, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {

					if (resuleCode == RESULTCODE.SUCCESS) {
						Body body = ResponseUtils.getBody(mContext, result);
						showBody(body);

					}
				}
			});
		} else {
			ShowToast.showShort(this, R.string.net_break);
		}

	}

	/*
	 * 展示当前的身高和体重
	 */
	protected void showBody(Body body) {
		if (body != null) {
			if (body.current_detail_userinfo != null) {
				mDHeight.setRealTimeValue(Float
						.parseFloat(body.current_detail_userinfo.height)+40);
				mDWeight.setRealTimeValue(Float
						.parseFloat(body.current_detail_userinfo.weight));
				mTvH.setText(body.current_detail_userinfo.height);
				mTvHBig.setText(body.current_detail_userinfo.height);
				mTvW.setText(body.current_detail_userinfo.weight);
				mTvWBig.setText(body.current_detail_userinfo.weight);
			}
			
			if(body.d_wei_hei!=null){
				
				if(body.d_wei_hei.d_height_value.contains("-")){
					mTvHAdd.setVisibility(View.GONE);
				}else{
					mTvHAdd.setText("长高了"+body.d_wei_hei.d_height_value);
				}
				if(body.d_wei_hei.d_weigth_value.contains("-")){
					mTvWAdd.setText("减轻了"+body.d_wei_hei.d_weigth_value);
				}else{
					mTvWAdd.setText("增重了"+body.d_wei_hei.d_weigth_value);
				}
			}else{
				mTvHAdd.setText("");
				mTvWAdd.setText("");
			}
		}

	}
}
